<?php
if(!defined('IN_DISCUZ')) {
    exit('Access Denied');
}

class table_er_account_user extends discuz_table
{
    public function __construct() {
        $this->_table = 'er_accountcenter_user';
        $this->_pk    = 'id';
        parent::__construct();
    }

    /**
     * 根据id查询
     * 
     * @param int $id ID
     * @return array 用户信息数组
     */
    public function fetch_by_id($id) {
        return DB::fetch_first("SELECT * FROM %t WHERE id=%d", array($this->_table, $id));
    }

    /**
     * 根据用户ID获取用户信息
     * @param int $uid 用户ID
     * @return array 用户信息数组
     */
    public function fetch_by_uid($uid) {
        return DB::fetch_first("SELECT * FROM %t WHERE uid=%d", array($this->_table, $uid));
    }

    /**
     * 获取用户列表
     */
    public function fetch_all_with_username($search = array(), $start = 0, $limit = 20) {
        $sql = "SELECT u.*, m.username FROM %t u LEFT JOIN %t m ON u.uid=m.uid WHERE 1";
        $params = array($this->_table, 'common_member');
        
        if(!empty($search['username'])) {
            $sql .= " AND m.username LIKE %s";
            $params[] = '%'.$search['username'].'%';
        }
        if(isset($search['status'])) {
            $sql .= " AND u.status=%d";
            $params[] = $search['status'];
        }
        
        $sql .= " ORDER BY u.id DESC LIMIT %d, %d";
        $params[] = $start;
        $params[] = $limit;
        
        return DB::fetch_all($sql, $params);
    }

    public function count_by_search($search = array()) {
        $sql = "SELECT COUNT(*) FROM %t u LEFT JOIN %t m ON u.uid=m.uid WHERE 1";
        $params = array($this->_table, 'common_member');
        
        if(!empty($search['username'])) {
            $sql .= " AND m.username LIKE %s";
            $params[] = '%'.$search['username'].'%';
        }
        if(isset($search['status'])) {
            $sql .= " AND u.status=%d";
            $params[] = $search['status'];
        }
        
        return DB::result_first($sql, $params);
    }

    /**
     * 插入用户
     * 
     * @param array $data 用户数据数组
     * @return int 插入的用户ID
     */
    public function insert($data, $return_insert_id = false, $replace = false, $silent = false, $unbuffered = false) {
        return DB::insert($this->_table, $data);
    }

    /**
     * 更新用户
     * 
     * @param int $id 用户ID
     * @param array $data 更新数据数组
     * @return int 更新的行数
     */
    public function update($id, $data, $unbuffered = false, $low_priority = false) {
        return DB::update($this->_table, $data, array($this->_pk => $id));
    }

    /**
     * 根据erling_unionid获取用户信息
     * 
     * @param string $erling_unionid 用户的erling_unionid
     * @return array 用户信息数组
     */
    public function fetch_by_erling_unionid($erling_unionid) {
        return DB::fetch_first("SELECT * FROM %t WHERE erling_unionid=%s ORDER BY id DESC LIMIT 1", array($this->_table, $erling_unionid));
    }

    /**
     * 根据ID删除用户
     * 
     * @param int $id 用户ID
     * @return int 删除的行数
     */
    public function delete_by_id($id) {
        return DB::delete($this->_table, array($this->_pk => $id));
    }

    /**
     * 根据erling_unionid删除用户
     * 
     * @param string $erling_unionid 用户的erling_unionid
     * @return int 删除的行数
     */
    public function delete_by_erling_unionid($erling_unionid) {
        return DB::delete($this->_table, array('erling_unionid' => $erling_unionid));
    }
}